<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatar_box">
        <img src="../assets/image/logo.png" />
      </div>
      <!-- 登录组件 -->
      <el-form class="login_form" :model="loginForm" :rules="rules" ref="LoginRef">
        <!-- 用户名 -->
        <el-form-item prop="account">
          <el-input prefix-icon="el-icon-user-solid" v-model="loginForm.account"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input prefix-icon="el-icon-lock" v-model="loginForm.password" show-password></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login()">登录</el-button>
          <el-button type="info" @click="resultLoginForm()">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        //这是登录表单的数据绑定对象
        loginForm: {
          account: "",
          password: ""
        },
        //表单数据的验证规则
        rules: {
          // 验证用户名是否合法
          accout: [{
              require: true,
              message: "请输入账号",
              trigger: "blur"
            },
            {
              min: 3,
              max: 10,
              message: "长度在3到10个字符",
              trigger: "blur"
            }
          ],
          // 验证密码是否合法
          password: [{
              require: true,
              message: "请输入密码",
              trigger: "change"
            },
            {
              min: 3,
              max: 10,
              message: "长度在3到10个字符",
              trigger: "blur"
            }
          ]
        }
      };
    },
    methods: {
      //重置表单函数
      resultLoginForm() {
        this.$refs.LoginRef.resetFields();
      },
      //表单的预验证
      login() {

        this.$refs.LoginRef.validate(async valid => {
          if (!valid) {
            return;
          } else {
            const result = await this.axios.post('/api/Login', this.loginForm);
            console.log(result);
            if (result.data.status !== 200) {
              this.$message.error("登录失败");
            } else {
              this.$message.success("登录成功");
              //将登录成功之后的token，保存到客户端的sessionStorage中
              window.sessionStorage.setItem("token", result.data.token);
              //将后台返回的用户权限等级存在sessionStrage中
              if(result.data.class === "root" || result.data.class === "user"){
                window.sessionStorage.setItem("Class",result.data.class);
              }if(result.data.username !== null){
                window.sessionStorage.setItem("username",result.data.username);
              }
              //后面通过编程式导航跳转到后台主页，路由地址是 /home
              this.$router.push({
                path: '/home'
              });
            }
          }
        })
      }
    }
  }
</script>

<style lang="less" scoped>
  .login_container {
    background: #a8ff78;  /* fallback for old browsers */
    background: -webkit-linear-gradient(to right, rgb(168, 255, 120), rgb(120, 255, 214));  /* Chrome 10-25, Safari 5.1-6 */
    background: linear-gradient(to right, rgb(168, 255, 120), rgb(120, 255, 214)); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
    // background-color: #bce672;
    height: 100%
  }

  .login_box {
    width: 450px;
    height: 300px;
    background-color: #FFFFFF;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    .avatar_box {
      height: 130px;
      width: 130px;
      border: 1px solid yellowgreen;
      border-radius: 50%;
      padding: 10px;
      box-shadow: 0 0 10px #ddd;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      background-color: white;

      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }
  }

  .btns {
    display: flex;
    justify-content: flex-end;
  }

  .login_form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
  }
</style>
